﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
using System.Windows.Forms.Design;

namespace Raider.WinForms.Actions.Design
{
    class CustomPropertyGrid : PropertyGrid
    {
        /// <summary>
        /// Gets or sets the renderer for <see cref="ToolStrip"/> objects, used by the current
        /// property grid.
        /// </summary>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new ToolStripRenderer ToolStripRenderer
        {
            get { return base.ToolStripRenderer; }
            set
            {
                if (value != null && value is ToolStripProfessionalRenderer)
                {
                    var proRenderer = (ToolStripProfessionalRenderer)value;
                    BackColor = proRenderer.ColorTable.ToolStripGradientBegin;
                }
                else
                {
                    ResetBackColor();
                }

                base.ToolStripRenderer = value;
            }
        }

        /// <summary>
        /// Gets the index of currently selected property tab.
        /// </summary>
        [Browsable(false)]
        public int SelectedTabIndex
        {
            get
            {
                var type = typeof(PropertyGrid);
                var selectedViewTabField = type.GetField("selectedViewTab", BindingFlags.Instance | BindingFlags.NonPublic);
                var selectedViewTab = (int)selectedViewTabField.GetValue(this);
                return selectedViewTab;
            }
        }

        /// <summary>
        /// Selects specified property tab by it's index.
        /// </summary>
        /// <param name="tabIndex">Index of the property tab.</param>
        public void SelectTab(int tabIndex)
        {
            var type = typeof(PropertyGrid);
            var method = type.GetMethod("SelectViewTabButton", BindingFlags.Instance | BindingFlags.NonPublic);
            var field = type.GetField("viewTabButtons", BindingFlags.Instance | BindingFlags.NonPublic);
            var buttons = (ToolStripButton[])field.GetValue(this);

            if (tabIndex >= 0 && tabIndex < buttons.Length)
            {
                method.Invoke(this, new object[] { buttons[tabIndex], true });
            }
        }

        private bool eventsTabAdded;

        /// <summary>
        /// Adds Events property tab (works only in the design-mode).
        /// </summary>
        public void AddEventsTab(ISite site)
        {
            if (eventsTabAdded)
                return;

            Site = site;
            PropertyTabs.AddTabType(typeof(EventsTab), PropertyTabScope.Global);

            // Get PropertyGridView.
            var type = typeof(PropertyGrid);
            var field = type.GetField("gridView", BindingFlags.Instance | BindingFlags.NonPublic);
            var gridView = (Control)field.GetValue(this);

            // Get GridViewEdit.
            var gridViewType = gridView.GetType();
            var editProperty = gridViewType.GetProperty("Edit", BindingFlags.Instance | BindingFlags.NonPublic);
            var edit = (TextBox)editProperty.GetValue(gridView, null);

            // Subscribe to event.
            edit.DoubleClick += OnGridViewMouseDoubleClick;
            eventsTabAdded = true;
        }

        private void OnGridViewMouseDoubleClick(object sender, EventArgs e)
        {
            if (PropertyItemDoubleClicked != null)
                PropertyItemDoubleClicked(this, e);
        }

        /// <summary>
        /// Occurs when user double-clicks some property grid item.
        /// </summary>
        public event EventHandler PropertyItemDoubleClicked;
    }
}
